//
//  ByBonusAddMinusView.m
//  caipiao_ios
//
//  Created by mc on 2018/9/5.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByBonusAddMinusView.h"

@implementation ByBonusAddMinusView

- (id)init{
    self = [super init];
    if (self) {
        
        UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:minusBtn];
        [minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(7);
            make.centerY.equalTo(self).offset(0);
            make.size.mas_equalTo(CGSizeMake(26, 26));
        }];
        [minusBtn setImage:[UIImage imageNamed:@"jian"] forState:UIControlStateNormal];
        [minusBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        minusBtn.tag = 100;
        
        
    
        UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:addBtn];
        [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-7);
            make.centerY.equalTo(self).offset(0);
            make.size.mas_equalTo(CGSizeMake(26, 26));
        }];
        [addBtn setImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
        [addBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        addBtn.tag = 101;
        
        
        UITextField *countLabel = [[UITextField alloc] init];
        [self addSubview:countLabel];
        [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self).offset(0);
            make.height.mas_equalTo(@26);
            make.left.equalTo(minusBtn.mas_right).offset(0);
            make.right.equalTo(addBtn.mas_left).offset(0);
        }];
        countLabel.text = @"1";
        countLabel.textColor = BY_TEXT_COLOR;
        countLabel.textAlignment = NSTextAlignmentCenter;
        countLabel.font = FONT(14);
        countLabel.keyboardType = UIKeyboardTypeNumberPad;
        countLabel.delegate = self;
        countLabel.layer.borderColor = BY_LINE_COLOR.CGColor;
        countLabel.layer.borderWidth = 1;
        self.countLabel = countLabel;
    }
    return self;
}


- (void)btnAction:(UIButton *)btn{
    if (btn.tag == 100){
        if (self.countLabel.text.integerValue > 1) {
            self.countLabel.text = [NSString stringWithFormat:@"%ld", self.countLabel.text.integerValue - 1];
            if (self.updateBlock) {
                self.updateBlock(btn.tag);
            }
        }
    }else if (btn.tag == 101){
        self.countLabel.text = [NSString stringWithFormat:@"%ld", self.countLabel.text.integerValue + 1];
        if (self.updateBlock) {
            self.updateBlock(btn.tag);
        }
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.integerValue < 1) {
        textField.text = @"1";
        [ByCoreToolAlert showOnlyText:@"最小为1"];
        if (self.updateBlock) {
            self.updateBlock(0);
        }
    }else{
        if (self.updateBlock) {
            self.updateBlock(0);
        }
    }
    
}

@end
